package com.galaxy.product.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.galaxy.common.core.utils.MapstructUtils;
import com.galaxy.common.core.utils.StringUtils;
import com.galaxy.common.mybatis.core.page.PageQuery;
import com.galaxy.common.mybatis.core.page.TableDataInfo;
import com.galaxy.product.domain.ProductDetail;
import com.galaxy.product.domain.bo.ProductDetailBo;
import com.galaxy.product.domain.vo.ProductDetailVo;
import com.galaxy.product.mapper.ProductDetailMapper;
import com.galaxy.product.service.IProductDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 商品详情Service实现类
 *
 * @author GalaxyMart
 */
@Service
@RequiredArgsConstructor
public class ProductDetailServiceImpl extends ServiceImpl<ProductDetailMapper, ProductDetail> implements IProductDetailService {

    private final ProductDetailMapper baseMapper;

    @DS("slave")
    @Override
    public TableDataInfo<ProductDetailVo> queryPageList(ProductDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductDetail> lqw = buildQueryWrapper(bo);
        Page<ProductDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        // 设置额外信息
        result.getRecords().forEach(this::setProductDetailExtraInfo);
        return TableDataInfo.build(result);
    }

    @DS("slave")
    @Override
    public List<ProductDetailVo> queryList(ProductDetailBo bo) {
        LambdaQueryWrapper<ProductDetail> lqw = buildQueryWrapper(bo);
        List<ProductDetailVo> list = baseMapper.selectVoList(lqw);
        // 设置额外信息
        list.forEach(this::setProductDetailExtraInfo);
        return list;
    }

    @DS("slave")
    @Override
    public ProductDetail getInfo(Long id) {
        ProductDetail detail = baseMapper.selectById(id);
        if (detail != null) {
            setProductDetailExtraInfo(detail);
        }
        return detail;
    }

    @DS("slave")
    @Override
    public ProductDetail getInfoByProductId(Long id) {
        ProductDetail detail = baseMapper.selectOne(new LambdaQueryWrapper<ProductDetail>()
            .eq(ProductDetail::getProductId, id));
        if (detail != null) {
            setProductDetailExtraInfo(detail);
        }
        return detail;
    }

    @Override
    public void add(ProductDetailBo bo) {
        ProductDetail add = MapstructUtils.convert(bo, ProductDetail.class);
        baseMapper.insert(add);
    }

    @Override
    public void update(ProductDetailBo bo) {
        ProductDetail update = MapstructUtils.convert(bo, ProductDetail.class);
        baseMapper.updateById(update);
    }

    @Override
    public void delete(Long id) {
        baseMapper.deleteById(id);
    }

    @Override
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            baseMapper.deleteById(id);
        }
    }

    private LambdaQueryWrapper<ProductDetail> buildQueryWrapper(ProductDetailBo bo) {
        LambdaQueryWrapper<ProductDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getProductId() != null, ProductDetail::getProductId, bo.getProductId());
        lqw.like(StringUtils.isNotBlank(bo.getSubtitle()), ProductDetail::getSubtitle, bo.getSubtitle());
        lqw.eq(bo.getUnitId() != null, ProductDetail::getUnitId, bo.getUnitId());
        lqw.eq(bo.getIsHot() != null, ProductDetail::getIsHot, bo.getIsHot());
        return lqw;
    }

    /**
     * 设置商品详情额外信息（单位名称、热销状态名称）
     */
    private void setProductDetailExtraInfo(ProductDetail detail) {
        if (detail != null) {
            // 设置单位名称（假设有单位表关联）
            if (detail.getUnitId() != null) {
                // 这里需要根据实际情况查询单位表，示例代码省略
                // detail.setUnitName(unitMapper.selectById(detail.getUnitId()).getName());
            }

            // 设置热销状态名称
            if (detail.getIsHot() != null) {
                detail.setIsHotName(detail.getIsHot() == 1 ? "是" : "否");
            }
        }
    }

    private void setProductDetailExtraInfo(ProductDetailVo detail) {
        if (detail != null) {
            // 设置单位名称（假设有单位表关联）
            if (detail.getUnitId() != null) {
                // 这里需要根据实际情况查询单位表，示例代码省略
                // detail.setUnitName(unitMapper.selectById(detail.getUnitId()).getName());
            }

            // 设置热销状态名称
            if (detail.getIsHot() != null) {
                detail.setIsHotName(detail.getIsHot() == 1 ? "是" : "否");
            }
        }
    }

    /**
     * 分页查询商品详情列表
     *
     * @param productDetail 查询条件
     * @param pageQuery 分页参数
     * @return 分页结果
     */
    @Override
    public TableDataInfo<ProductDetail> selectPage(ProductDetail productDetail, PageQuery pageQuery) {
        LambdaQueryWrapper<ProductDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(productDetail.getProductId() != null, ProductDetail::getProductId, productDetail.getProductId());
        lqw.like(productDetail.getSubtitle() != null, ProductDetail::getSubtitle, productDetail.getSubtitle());
        lqw.eq(productDetail.getIsHot() != null, ProductDetail::getIsHot, productDetail.getIsHot());
        Page<ProductDetail> page = page(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }

    /**
     * 根据商品ID查询商品详情
     *
     * @param productId 商品ID
     * @return 商品详情
     */
    @Override
    public ProductDetail selectByProductId(Long productId) {
        LambdaQueryWrapper<ProductDetail> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductDetail::getProductId, productId);
        return getOne(lqw);
    }

    /**
     * 新增商品详情
     *
     * @param productDetail 商品详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertProductDetail(ProductDetail productDetail) {
        return save(productDetail);
    }

    /**
     * 修改商品详情
     *
     * @param productDetail 商品详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateProductDetail(ProductDetail productDetail) {
        return updateById(productDetail);
    }

    /**
     * 删除商品详情
     *
     * @param id 商品详情ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteProductDetailById(Long id) {
        return removeById(id);
    }
}
